package com.example.provider;

import com.example.provider.constant.MqConstant;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.junit.jupiter.api.Test;

/**
 * @description: TODO
 * @author: uu
 * @date: 2023/12/10 14:25
 * @version: 1.0
 */
public class DMsRocket {
    @Test
    void DMs() throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("ms-group");
        producer.setNamesrvAddr(MqConstant.NAME_SRV_ADDR);
        producer.start();
        Message message = new Message("dmsMessage", "dms message body".getBytes());
        // 给消息设置延迟时间
        message.setDelayTimeLevel(2);
        producer.send(message);
        producer.shutdown();

    }

    @Test
    void msgConsumer() throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("dmsConsumer-group");
        consumer.setNamesrvAddr(MqConstant.NAME_SRV_ADDR);
        consumer.subscribe("dmsMessage", "*");
        consumer.registerMessageListener((MessageListenerConcurrently) (msg, context)->{
            System.out.println(new String(msg.get(0).getBody()));
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
        System.in.read();
    }
}
